package com.guigu.tms.dispatch.Task;

import com.guigu.tms.commons.aggregation.entity.TaskTransport;
import com.guigu.tms.commons.dispatch.entity.OrderClassify;
import com.guigu.tms.dispatch.dto.OrderLineSimpleDTO;
import com.guigu.tms.dispatch.dto.OrderLineTripsTruckDriverDTO;
import com.guigu.tms.dispatch.service.IBusinessOperationService;
import com.guigu.tms.dispatch.service.ITaskOrderClassifyService;
import com.guigu.tms.dispatch.service.ITaskRoutePlanningService;
import com.guigu.tms.dispatch.service.ITaskTripsSchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("dispatchTask")
public class DispatchTask {

    @Autowired
    private ITaskOrderClassifyService taskOrderClassifyService;

    @Autowired
    private ITaskRoutePlanningService taskRoutePlanningService;

    @Autowired
    private IBusinessOperationService businessOperationService;

    @Autowired
    private ITaskTripsSchedulingService taskTripsSchedulingService;

    /**
     * 智能调度
     * @param businessId 业务编号
     * @param params 参数
     * @param jobId 任务编号
     * @param logId 日志id
     */
    public void run(String businessId, String params, String jobId, String logId) {
        System.out.println(businessId+" "+params+" "+jobId+" "+logId);
        System.out.println("智能调度正在执行");
        //订单分类
        List<OrderClassify> orderClassifys = taskOrderClassifyService.execute(businessId, jobId, logId);
        //线路规划
        List<OrderLineSimpleDTO> orderLineSimpleDTOS = taskRoutePlanningService.execute(orderClassifys, businessId, jobId, logId, params);
        //创建运输任务，关联运单
        Map<String, TaskTransport> taskTransportMap = businessOperationService.createTransportOrderTask(orderLineSimpleDTOS);
        //分派车次 车辆 司机
        List<OrderLineTripsTruckDriverDTO> orderLineTripsTruckDrivers = taskTripsSchedulingService.execute(orderLineSimpleDTOS, businessId, jobId, logId, params);
        //创建司机作业任务
        businessOperationService.updateTransportTask(orderLineTripsTruckDrivers,taskTransportMap);

        System.out.println(jobId+"调度完成！");
    }
}
